//
//  AppLaunchManager.swift
//  kano
//
//  Created by Frank on 2025/6/9.
//

import Foundation

class AppLaunchManager {
    static let shared = AppLaunchManager()
    private let userDefaults = UserDefaults.standard
    
    private enum Keys {
        static let hasLaunchedBefore = "com.lendSafe.app.hasLaunchedBefore"
        static let appVersion = "com.lendSafe.app.version"
    }
    
    // 检查是否是首次安装启动
    var isFirstLaunch: Bool {
        get {
            // 检查是否有记录
            if !userDefaults.bool(forKey: Keys.hasLaunchedBefore) {
                // 标记为已启动
                userDefaults.set(true, forKey: Keys.hasLaunchedBefore)
                userDefaults.synchronize()
                return true
            }
            return false
        }
    }
    
    // 检查是否是当前版本的首次启动
    var isFirstLaunchOfNewVersion: Bool {
        get {
            // 获取当前版本
            guard let currentVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String else {
                return false
            }
            
            // 获取上次记录的版本
            let previousVersion = userDefaults.string(forKey: Keys.appVersion)
            
            // 版本不同（首次启动新版本或从未记录过版本）
            if previousVersion != currentVersion {
                // 更新版本记录
                userDefaults.set(currentVersion, forKey: Keys.appVersion)
                userDefaults.synchronize()
                return true
            }
            
            return false
        }
    }
}
